package com.gscitysfy.cus.drain.utils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @ClassName ParamsValidUtil
 * @Description: 检查对象是否符合注解规范工具类
 * @Author: wuxinxin
 * @CreateDate: 2022/7/29 14:34
 * @Version: 1.0
 */
public class ParamsValidUtil {

    /**
     * @auther: wuxinxin
     * @date: 2022/7/29 14:34
     * @Description: 检查对象参数是否符合注解规范
     * @param t
     * @return java.util.Map<java.lang.String,java.lang.String>
     */
    public static <T> String checkObjectParamsValid(T t) {
        String message = null;
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<T>> violations = validator.validate(t);
        if (violations != null && violations.size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            violations.forEach(
                    e -> stringBuilder.append(e.getPropertyPath() + ":" +e.getMessage()).append(", "));
            message =  stringBuilder.toString().substring(0, stringBuilder.length() - 2);
            return message;
        }
        return message;
    }

}
